package com.jianan.mis.app.overseastudy;

import java.awt.Dimension;
import java.awt.Toolkit;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.jianan.mis.data.domain.OsContract;
import com.jianan.mis.service.OsContractService;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.sys.SpringBeanFactory;

public class OsContractDialog extends Dialog {

	private OsContractComposite contractComposite;
	
	private OsContract osContract;
	
	private boolean readonly;

	/**
	 * Create the dialog
	 * @param parentShell
	 */
	public OsContractDialog(Shell parentShell) {
		super(parentShell);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("留学签约表");
	}

	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {

		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		composite.setLayout(gridLayout);
	
		final Label left_label = new Label(composite, SWT.NONE);
		final GridData gd_label = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		left_label.setLayoutData(gd_label);

		final ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.V_SCROLL | SWT.H_SCROLL);
		scrolledComposite.setAlwaysShowScrollBars(false);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int height = screenSize.height - 150 > 585 ? 585 : screenSize.height - 150;
		scrolledComposite.setLayoutData(new GridData(SWT.DEFAULT, height));

		contractComposite = new OsContractComposite(scrolledComposite, SWT.NONE);
		contractComposite.setMode(1);
		contractComposite.setSize(contractComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		contractComposite.setOsContract(osContract);
		if (readonly) {
			contractComposite.setEnabled(false);
		} else {
			contractComposite.setEnabled(true);
		}
		scrolledComposite.setContent(contractComposite);

		final Label right_label = new Label(composite, SWT.NONE);
		final GridData gd_label_1 = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		right_label.setLayoutData(gd_label_1);
		
		//
		return composite;
	}
	
	public void setOsContract(OsContract osContract) {
		this.osContract = osContract;
	}
	
	public OsContract getOsContract() {
		return osContract;
	}
	
	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	/**
	 * Create contents of the button bar
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		if (readonly) {
			createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		} else {
			createButton(parent, IDialogConstants.OK_ID, "保存", true);
			createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		}
	}
	
	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(730, 690);
	}
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			try {
				// 更新留学咨询表
				OsContract updatedContract = contractComposite
						.getOsContract();
				if (updatedContract == null) {
					return;
				}
				if (MessageDialog.openConfirm(getShell(),
						IMessageKeys.CONFIRM, "确认更新该留学签约表信息吗?")) {
					OsContractService osContractService = (OsContractService) SpringBeanFactory
							.getBean("osContractService");
					osContractService.update(updatedContract);
					MessageDialog.openInformation(getShell(),
							IMessageKeys.SUCCESS, "档案号为"
									+ updatedContract.getId()
									+ "的留学签约表保存成功!");
					osContract = updatedContract;
				} else {
					return;
				}
			} catch (Exception ex) {
				MessageDialog.openError(getShell(), IMessageKeys.FAIL, ex
						.getMessage());
			}
		}
		super.buttonPressed(buttonId);
	}

}
